package com.jsmtr.rms.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jsmtr.rms.entity.*;
import com.jsmtr.rms.service.*;
import com.jsmtr.rms.utils.StringTools;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jsmtr.rms.utils.EnumTools;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/Do/NZ")
public class DoNZController {

    @Resource
    private IUtilService utilService;

    @Resource
    private IProjectService projectService;

    @Resource
    private INZService nzService;

    @Resource
    private IDoSZService szService;

    @Resource
    private IDoBSService bsService;
    @Resource
    private IDoMSService msService;

    //简历筛选主界面
    @RequestMapping("/")
    public String Index(HttpServletRequest request, Model model) {
        try {
            model.addAttribute("path", request.getContextPath());
            List<ProjectInfo> projects = projectService.getProjectInfos(EnumTools.RecruitType.内部招聘.ordinal());
            StringBuilder pros = new StringBuilder();
            pros.append("<option value=''>直接选择或搜索选择</option>");
            for (ProjectInfo project : projects) {
                if (project.getState() != EnumTools.ProjectState.编辑中.ordinal()){
                    pros.append("<option value='" + project.getId() + "'>" + project.getPname() + "</option>");
                }
            }
            model.addAttribute("projects", pros);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Do/Internal/index";
    }

    //获得招聘方案责任部门列表
    @RequestMapping("/getdept")
    public void getdept(HttpServletRequest request, HttpServletResponse response) {
        try {
            String pid = request.getParameter("pid").trim();
            List<ProjectInfoFlow> flows = null;

            BaseUser user = utilService.getSessionUser(request);
            StringBuilder pros = new StringBuilder();

            //当前用户是招聘主管部门
            if (utilService.getConfig("hrcode").equals(user.getUdept())) {
                flows = projectService.getAllFlowDept(pid, 2);
                for (ProjectInfoFlow flow : flows) {
                    pros.append("<option value='" + flow.getDeptcode() + "'>" + flow.getDeptname() + "</option>");
                }
            } else {
                flows = projectService.getThisFlowDept(pid, user.getUdept());
                pros.append("<option value='" + flows.get(0).getDeptcode() + "'>" + flows.get(0).getDeptname() + "</option>");
            }

            utilService.printTextResult(pros.toString(), response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printTextResult("", response);
        }
    }

    //获得招聘实施流程信息
    @RequestMapping("/getFlows")
    public void getFlows(HttpServletRequest request, HttpServletResponse response) {
        try {
            String pid = request.getParameter("pid").trim();
            String deptcode = request.getParameter("deptcode").trim();
            List<ProjectInfoFlow> flows = projectService.getThisFlowDept(pid, deptcode);

            StringBuilder result = new StringBuilder();
            int index = 1;
            for (ProjectInfoFlow flow : flows) {
                int ftype = flow.getFid();
                String url = null;
                /*if (ftype == EnumTools.NZFlowState.职位申请.ordinal()) {
                    url = "NZApply/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                } else */if (ftype == EnumTools.NZFlowState.报名筛选.ordinal()) {
                    url = "NZResume/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                } else if (ftype == EnumTools.NZFlowState.面试.ordinal()) {
                    url = "NZInterview/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                } else if (ftype == EnumTools.NZFlowState.笔试.ordinal()) {
                    url = "NZExam/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                } else if (ftype == EnumTools.NZFlowState.录用.ordinal()) {
                    url = "NZEmploy/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                } else {
                    url = "";
                }
                result.append("<div class='cont cont" + flow.getState() + "'>");
                result.append("<a href='" + request.getContextPath() + "/Do/" + url + "'>");
                result.append("<span class='num'>" + index + "</span>");
                result.append("<span class='txt'>" + flow.getNickname() + "</span>");
                result.append("</a></div>");
                index++;
            }
            result.append("<div class='clear'></div>");
            utilService.printTextResult(result.toString(), response);
        } catch (Exception e) {
            utilService.printTextResult("", response);
        }
    }

    //[start] 获得招聘实施流程信息
    @RequestMapping(value = "/getFlowsJson",produces = "application/json;charset=utf-8")
   // public @ResponseBody
   // String getFlowsJson(HttpServletRequest request, HttpServletResponse response)
    public @ResponseBody String getFlowsJson(HttpServletRequest request, HttpServletResponse response){
        try {

            String pid = request.getParameter("pid").trim();
            String deptcode = request.getParameter("deptcode").trim();
            List<ProjectInfoFlow> flows = projectService.getThisFlowDept(pid, deptcode);

            StringBuilder result = new StringBuilder();
            int index = 1;
            result.append("[");
            for (ProjectInfoFlow flow : flows) {
                int ftype = flow.getFid();
                String fname=flow.getFname()==null?flow.getNickname():flow.getFname();
                String url = null;
                if (fname.equalsIgnoreCase(EnumTools.NZFlowState.报名筛选.name())) {
                    url = "NZResume/";
                } else if (fname.equalsIgnoreCase(EnumTools.NZFlowState.面试.name())) {
                    url = "NZInterview/";
                } else if (fname.equalsIgnoreCase(EnumTools.NZFlowState.笔试.name())) {
                    url = "NZExam/";
                } else if (fname.equalsIgnoreCase(EnumTools.NZFlowState.录用.name()))  {
                    url = "NZEmploy/";
                } else {
                    url = "";
                }

                String display="";
                String displayTab="";
                if (StringTools.trimNotEmpty(flow.getDisplay())){
                    if (flow.getDisplay().equalsIgnoreCase("是")){
                        display="显示";
                    }else{
                        display="不显示";
                        displayTab="(否)";
                    }
                }


                String href=request.getContextPath() + "/Do/" + url;
                result.append("{\"fid\":\""+flow.getId()+"\",\"state\":\""+flow.getState()+"\",\"num\":\""+index+"\",\"txt\":\""+flow.getNickname()+displayTab+"\",\"url\":\""+href+"\"},");
                index++;
            }
            result=result.replace(result.lastIndexOf(","),result.length(),"");
            result.append("]");

           // utilService.printTextResult(result.toString(), response);
            return result.toString();
        } catch (Exception e) {
            utilService.printTextResult("", response);
            return "";
        }

    }
    //[end]

    @RequestMapping("/RecommendPost")
    public String RecommendPost(HttpServletRequest request, Model model) {
        try {
            model.addAttribute("path", request.getContextPath());
            String pid = request.getParameter("pid").trim();
            String fid = request.getParameter("fid").trim();
            String deptcode = request.getParameter("deptcode").trim();

            model.addAttribute("pid", pid);
            model.addAttribute("fid", fid);
            model.addAttribute("deptcode", deptcode);

            //招聘岗位
            List<ProjectInfoJobs> jobs = projectService.getjobsbypid(pid);
            StringBuilder dstr = new StringBuilder();
            for (ProjectInfoJobs job : jobs) {
                dstr.append("<option value='" + job.getId() + "'>" + job.getJobname() + "</option>");
            }
            model.addAttribute("jobs", dstr);

            String[] ids = request.getParameterValues("id");
            List<DNZResume> resumes = nzService.getDResumes(ids);
            StringBuffer rs = new StringBuffer();
            StringBuffer _ids = new StringBuffer();
            for (DNZResume resume : resumes) {
                if (resume.getPid().equals(pid)
                        && resume.getFqstate().equals("未放弃")
                        && resume.getNrid() == null) {
                    rs.append("<tr><td>" + resume.getCode() + "</td><td>" + resume.getJname() + "</td><td>" + resume.getName() + "</td>");
                    rs.append("<td>" + resume.getSexname() + "</td><td>" + resume.getCardno() + "</td></tr>");

                    _ids.append("<input type='hidden' name='id' value='" + resume.getId() + "'/>");
                }
            }
            model.addAttribute("rs", rs);
            model.addAttribute("ids", _ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Do/DNZResume/rpost";
    }

    @RequestMapping(value = "/PublishResultAction", method = RequestMethod.POST)
    public void PublishResultAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String[] ids = request.getParameterValues("id");//人员信息ID
            if (ids != null) {
                String fid = request.getParameter("fid").trim();//招聘流程ID
                String deptcode = request.getParameter("deptcode").trim();//责任部门编码
                BaseUser usr = utilService.getSessionUser(request);
                if (usr.getUdept().equals(deptcode)) {
                    //流程是否结束
                    if (utilService.FlowIsDoing(fid)) {
                        List editlist = new ArrayList();
                        List<DUserResume> resumes = szService.getResumes(ids);
                        ProjectInfoFlow nFlow = szService.getCurrentFlow(fid);
                        if (nFlow.getFid() == EnumTools.NZFlowState.报名筛选.ordinal()) {
                            for (DUserResume user : resumes) {
                                if (user.getOp1() == null || user.getOp1().equals("未公布")) {
                                    user.setOp1("已公布");
                                }else {
                                    user.setOp1("未公布");
                                }
                                editlist.add(user);
                            }
                        }
                        if (nFlow.getFid() == EnumTools.NZFlowState.笔试.ordinal()) {
                            List<DoBSUser> bsUsers = bsService.getBsUsers(ids, fid);
                            for (DoBSUser user : bsUsers) {
                                if (user.getOp6() == null || user.getOp6().equals("未公布")) {
                                    user.setOp6("已公布");
                                }else {
                                    user.setOp6("未公布");
                                }
                                editlist.add(user);
                            }
                        }
                        if (nFlow.getFid() == EnumTools.NZFlowState.面试.ordinal()) {
                            List<DoMSUser> msUsers = msService.getMsUsers(ids, fid);
                            for (DoMSUser user : msUsers) {
                                if (user.getOp7() == null || user.getOp7().equals("未公布")) {
                                    user.setOp7("已公布");
                                }else {
                                    user.setOp7("未公布");
                                }
                                editlist.add(user);
                            }
                        }
                        if (nFlow.getFid() == EnumTools.NZFlowState.录用.ordinal()) {
                            for (DUserResume user : resumes) {
                                if (user.getOp5() == null || user.getOp5().equals("未公布")) {
                                    user.setOp5("已公布");
                                }else {
                                    user.setOp5("未公布");
                                }
                                editlist.add(user);
                            }
                        }
                        /*if (nFlow.getFid() == EnumTools.NZFlowState.资料审核.ordinal()) {
                            for (DUserResume user : resumes) {
                                if (user.getOp2() == null || user.getOp2().equals("未公布")) {
                                    user.setOp2("已公布");
                                }else {
                                    user.setOp2("未公布");
                                }
                                editlist.add(user);
                            }
                        }
                        if (nFlow.getFid() == EnumTools.NZFlowState.背景调查.ordinal()) {
                            for (DUserResume user : resumes) {
                                if (user.getOp3() == null || user.getOp3().equals("未公布")) {
                                    user.setOp3("已公布");
                                }else {
                                    user.setOp3("未公布");
                                }
                                editlist.add(user);
                            }
                        }
                        if (nFlow.getFid() == EnumTools.NZFlowState.体检.ordinal()) {
                            for (DUserResume user : resumes) {
                                if (user.getOp4() == null || user.getOp4().equals("未公布")) {
                                    user.setOp4("已公布");
                                }else {
                                    user.setOp4("未公布");
                                }
                                editlist.add(user);
                            }
                        }
                        if (nFlow.getFid() == EnumTools.NZFlowState.实作.ordinal()) {
                            List<DoOperationUser> szUsers = szService.getSZUsers(fid, ids);
                            for (DoOperationUser user : szUsers) {
                                if (user.getOp8() == null || user.getOp8().equals("未公布")) {
                                    user.setOp8("已公布");
                                }else {
                                    user.setOp8("未公布");
                                }
                                editlist.add(user);
                            }
                        }*/

                        if (editlist.size() > 0) {
                            utilService.editList(editlist);
                            utilService.printJsonResult(true, "操作成功！", "no", response);
                        } else {
                            utilService.printJsonResult(false, "当前选中的信息不能执行此操作！", "no", response);
                        }
                    } else {
                        utilService.printJsonResult(false, "当前流程不支持此操作！", "no", response);
                    }
                } else {
                    utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
                }
            } else {
                utilService.printJsonResult(false, "请选择人员信息！", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }
}
